package cn.lzj.controller.base;

import cn.lzj.Utils.AliSmsUtil;
import cn.lzj.pojo.Cart;
import cn.lzj.pojo.ProductBase;
import cn.lzj.pojo.User;
import cn.lzj.service.User.CartService;
import cn.lzj.service.User.ProductService;
import cn.lzj.service.base.LoginService;

import cn.lzj.service.base.RegisterService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class loginController {

    @Resource
    private LoginService loginService;
    @Resource
    private RegisterService registerService;

    @Resource
    private indexController indexController;

    @Resource
    private ProductService productService;

    @Resource
    private CartService cartService;

    String code = "";

    @PostMapping(value = "/AccountAndPassword")
    public String login(String AccountNo, String password, Model model, HttpServletRequest request){
        User user = loginService.selectByAccountNoAndPasswordService(AccountNo, password);
        HttpSession session = request.getSession();
        if(user != null){
            session.setAttribute("user",user);

//            model.addAttribute("user",user);

            String id = session.getId();
            System.out.println("login session"+id);

            //防止主键出现空的情况
            Cart cart = cartService.getCart(user);
            user.setCart(cart);
            return indexController.getProductByPage(1,user.getUserId(),
                    "","","","" ,model,request);
        }
        model.addAttribute("type","text");
        model.addAttribute("error","账号或者密码错误");
        return "login";
    }


    @PostMapping("/loginByPhone")
    public String loginByPhone(String telephone,String check){
        User user = loginService.loginByPhone(telephone);
        if(user != null && check.equals(code)){
            return "index";
        }else {
            return "login";
        }
    }


    @GetMapping("/getLoginByPhone/{phone}")
    public void getLoginByPhone(@PathVariable("phone") String phone){
        code = registerService.getCodeNum(phone);
        System.out.println(code);
        AliSmsUtil.sendSmsCode(phone,code);
    }



    @GetMapping("/login")
    public String login(Model model){
        model.addAttribute("type","hidden");
        return "login";
    }


    //失去焦点事件
    @GetMapping("/selectByAccountNo/{AccountNo}")
    @ResponseBody
    public String selectByAccountNo(@PathVariable("AccountNo") String AccountNo){
        System.out.println(AccountNo);
        User user = loginService.selectByAccountNoService(AccountNo);
        if(user != null){
            //用户名存在，可以登录
            return "1";
        }else {
            //用户名不存在，请先注册
            return "0";
        }
    }
}
